package com.sk.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sk.constant.Constant;
import com.sk.enums.PlatformEnum;
import com.sk.service.IWechatService;
import com.sk.stone.utils.RedisUtil;
import com.sk.stone.utils.WechatNoticeUtils;
import com.sk.util.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author jiaok
 * @Date 2024-04-24 17:30
 */
@Service
@Slf4j
public class WechatServiceImpl  implements IWechatService {
    @Autowired
    private RedisUtil redisUtil;

//    @Value("${applet.appid}")
    private String appletAppId;
//    @Value("${applet.appSecret}")
    private String appletAppSecret;
//    @Value("${wechat.service.appid}")
    public  String wechatServiceAppid;
//    @Value("${wechat.service.secret}")
    public  String wechatServiceSecret;

    @Value("${weixin.appId}")
    private String appId;
    @Value("${weixin.interfaceUrl}")
    private String interfaceUrl;
    @Value("${weixin.appSecret}")
    private String appSecret;
//    @Value("${weixin.chatUrl}")
    private String chatUrl;

    @Override
    public JSONObject login2(String code) {
        log.info("==============小程序登录方法开始================");
        String url = interfaceUrl + "/sns/jscode2session?appid="
                + appId + "&secret=" + appSecret
                + "&js_code=" + code + "&grant_type=authorization_code";

        JSONObject message= new JSONObject();

        try {
            String result = HttpClientUtil.doGet(url);
            if (org.apache.commons.lang3.StringUtils.isNotBlank(result) ) {
                JSONObject jsonObject = JSONObject.parseObject(result);
                return jsonObject;
            }
        } catch (Exception e) {
            log.error("微信服务器请求错误", e);
        }
        log.info("==============小程序登录方法结束================");
        return message;

    }

    /**
     * 消费code来换取用户手机号
     * @param code
     * @return
     */
    @Override
    public JSONObject getPhoneNumber(String code) {
        log.info("==============小程序消费code来换取用户手机号方法开始================");
        String accessToken = getAccessToken(PlatformEnum.PLATEFORM_1.getCode());
        String url = interfaceUrl + "/wxa/business/getuserphonenumber?access_token=" + accessToken+ "&code=" + code ;

        Map<String,Object> body = new HashMap<>();
        body.put("code",code);

        JSONObject message= new JSONObject();

        try {
            String result = HttpClientUtil.doPostJson(url,JSONObject.toJSONString(body));
            if (org.apache.commons.lang3.StringUtils.isNotBlank(result)) {
                message = JSONObject.parseObject(result);
            }
        } catch (Exception e) {
            log.error("微信服务器请求错误", e);
        }
        log.info("==============小程序消费code来换取用户手机号方法结束================");
        return message;

    }

    /**
     * 获取不同平台的接口调用凭证:access_token
     * appid和appsecret到小程序后台获取，当然也可以让小程序开发人员给你传过来
     * @param platform 系统自定义平台标识
     * @return
     */
    @Override
    public  String getAccessToken(Integer platform) {
//        String key= Constant.KEY_ACCESS_TOKEN+platform;
//        boolean re = redisUtil.hasKey(key);
//        if(re){
//            return redisUtil.get(key);
//        }
//        String appid="";
//        String appSecret="";
//
//        if(platform.equals(PlatformEnum.PLATEFORM_1.getCode())){
//            appid=appletAppId;
//            appSecret=appletAppSecret;
//        }else if(platform.equals(PlatformEnum.PLATEFORM_2.getCode())){
//            appid=wechatServiceAppid;
//            appSecret=wechatServiceSecret;
//        }
//        //获取access_token
//        String token =  WechatNoticeUtils.getAccessToken(appid,appSecret);
//        redisUtil.set(key,token,Constant.ACCTOKEN_VALID_TIME);
//        return token;
        return null;
    }

}
